package annotation;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class ParseAnno {
	
	public static void main(String[] args) throws ClassNotFoundException {
		
		
		Class clazz = Class.forName("annotation.Test");
		
		//找到属性上的注解
		Field[] fields = clazz.getDeclaredFields();//能得到私有属性
		for(Field field : fields) {
			Annotation[] anns = field.getAnnotations();
			for(Annotation anno : anns) {
				if(anno instanceof Description) {
					Description d = (Description) anno;
					System.out.println(d.age());
					System.out.println(d.value());
				}
			}
		}
		
		
		//找到方法上的注解
		Method[] methods = clazz.getMethods();
		for(Method method : methods) {
			Annotation[] anns = method.getAnnotations();
			for(Annotation anno : anns) {
				if(anno instanceof Description) {
					Description d = (Description) anno;
					System.out.println(d.age());
					System.out.println(d.value());
				}
			}
		}
		
		//另一种方法
		for(Method method : methods) {
			Boolean existDes = method.isAnnotationPresent(Description.class);
			if(existDes) {
				Description d = (Description) method.getAnnotation(Description.class);
				System.out.println(d.age());
				System.out.println(d.value());
			}
		}
		
		
	}

}
